package com.hl.card.modular.pack.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.hl.card.common.persistence.model.TextAddress;
import com.hl.card.common.persistence.model.TextAddressType;
import com.hl.card.common.persistence.model.TextUser;
import com.hl.card.modular.pack.service.TextAddressService;
import com.mongodb.BasicDBObject;

@Service
public class TextAddressServiceImpl implements TextAddressService{
	@Autowired
	private MongoTemplate mongoTemplate;
	
    private static String COLLECTION_NAME="textAddress";
    private static String COLLECTION_TYPE_NAME="textAddressType";
    
	/**
	 * 根据用户ID获取所有的地址本
	 * @param user_id
	 * @return   
	 */
    @Override
	public TextUser getTextUserByUserId(String user_id,int currentPage,int pageSize) {
   	    Query query = Query.query(Criteria.where("user_id").is(user_id));
		TextUser result=mongoTemplate.findOne(query,TextUser.class,COLLECTION_NAME);
		if(result==null)
		{
			result=new TextUser();
			result.setUser_id(user_id);
			result.setTextAddressList(new ArrayList<TextAddress>());
			return result;
		}
		int totalPage = (result.getTextAddressList().size()  +  pageSize  - 1) / pageSize;	
		result.setTextAddressList(getPageList(result.getTextAddressList(),currentPage,pageSize,totalPage));
        return result;
	}
	
	@Override
	public long getCount(String user_id,String address)
	{
		 Query query = Query.query(Criteria.where("user_id").is(user_id).and("textAddressList.address").is(address));
		 return mongoTemplate.count(query,TextUser.class,COLLECTION_NAME);
	}
	public List<TextAddressType> getTextAddressType()
	{
		 return mongoTemplate.findAll(TextAddressType.class,COLLECTION_TYPE_NAME);
	}
	/**
	 * 删除用户ID中的某一个地址本
	 * @param user_id
	 * @param address
	 */
	public void deleteAddressText(String user_id,String address)
	{
		 Query query = Query.query(Criteria.where("user_id").is(user_id).and("textAddressList.address").is(address));
		 Update update = new Update();
		 BasicDBObject s = new BasicDBObject();
         s.put("address", address);
		 update.pull("textAddressList", s);
	     //update.unset("textAddressList.$");
		 mongoTemplate.updateFirst(query, update, TextUser.class,COLLECTION_NAME);   
	}
	
	
	/**
	 * 修改用户ID中的某一个地址本
	 * @param user_id
	 * @param address
	 */
	public  void  updateAddressText(String user_id,String oldAddress,String address,String remark,String userName,String type)
	{
		 Query query = Query.query(Criteria.where("user_id").is(user_id).and("textAddressList.address").is(oldAddress));
		 Update update=new Update();
		 update.set("textAddressList.$.address", address);
		 update.set("textAddressList.$.remark", remark);
		 update.set("textAddressList.$.userName", userName);
		 update.set("textAddressList.$.type", type);
		 mongoTemplate.updateFirst(query, update, TextUser.class,COLLECTION_NAME); 
	}
	
	
	/**
	 * 新增用户ID中的某一个地址本
	 * @param user_id
	 * @param address
	 * @param remark
	 */
	public void  insertAddressText(String user_id,String address,String remark,String userName,String type)
	{
		Query query = Query.query(Criteria.where("user_id").is(user_id));
	    TextAddress textAddress=new TextAddress();
	    textAddress.setAddress(address);
	    textAddress.setRemark(remark);
	    textAddress.setUserName(userName);
	    textAddress.setType(type);
		Update update = new Update();
		update.addToSet("textAddressList", textAddress);
		mongoTemplate.upsert(query, update, COLLECTION_NAME);
	}

	/**
	
	     * 分页方法
	
	     *
	
	     * @param list
	
	     *            源数据
	
	     * @param currentPage
	
	     *            当前页
	
	     * @param maxNum
	
	     *            每页显示几条
	
	     * @param pageNum
	
	     *            总页数
	
	     * @return
	
	     */
	
	    public static List<TextAddress> getPageList(List<TextAddress> list, int currentPage, int maxNum,
	
	            int pageNum) {
	
	 
	
	        int fromIndex = 0; // 从哪里开始截取
	
	        int toIndex = 0; // 截取几个
	
	        if (list == null || list.size() == 0)
	
	            return null;
	
	        // 当前页小于或等于总页数时执行
	
	        if (currentPage <= pageNum && currentPage != 0) {
	
	            fromIndex = (currentPage - 1) * maxNum;
	
	 
	
	            if (currentPage == pageNum) {
	
	                toIndex = list.size();
	
	                 
	
	            } else {
	
	                toIndex = currentPage * maxNum;
	
	            }
	
	 
	
	        }
	
	        return list.subList(fromIndex, toIndex);
	
	    }
   
}
